package com.ccys.cms.service.corporateAndInsurance.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.corporateAndInsurance.DeeplinkInsuranceService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.DeeplinkInsuranceDto;
import com.ccys.db.entity.TblDeeplinkInsurance;
import com.ccys.db.mapper.TblDeeplinkInsuranceMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.corporateAndInsurance.DeeplinkInsuranceListSelectVo;
import com.ccys.db.vo.corporateAndInsurance.DeeplinkInsurancePageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 保险深度链接接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 16:04
 **/

@Service
public class DeeplinkInsuranceServiceImpl extends BaseServiceImpl<TblDeeplinkInsurance> implements DeeplinkInsuranceService {

    @Resource
    private TblDeeplinkInsuranceMapper tblDeeplinkInsuranceMapper;

    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveDeeplinkInsurance(DeeplinkInsuranceDto dto) {
        //校验字段值
        Kit.allParamValid(dto, "name", "type", "mobilePlatform", "identifier", "status", "sessionTimeout", "timeoutWarning");
        //校验是否重复
        Example example = new Example(TblDeeplinkInsurance.class);
        example.createCriteria().andEqualTo("identifier", dto.getIdentifier()).andEqualTo("delFlag", 0);
        //判断是否为修改 - 修改需要排除掉自身
        if (ObjectUtil.isNotEmpty(dto.getIdentifier())) {
            example.createCriteria().andNotEqualTo("id", dto.getId());
        }
        //检查是否重复 - 重复则返回错误信息
        if (ObjectUtil.isNotEmpty(tblDeeplinkInsuranceMapper.selectOneByExample(example))) {
            return ResponseResult.error("This Deeplink identifier already exists");
        }
        //以上都没问题则开始新增或修改 - 并直接返回
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteDeeplinkInsurance(DeeplinkInsuranceDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblDeeplinkInsurance tblDeeplinkInsurance = new TblDeeplinkInsurance();
        tblDeeplinkInsurance.setId(dto.getId());
        tblDeeplinkInsurance.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblDeeplinkInsuranceMapper.updateByPrimaryKeySelective(tblDeeplinkInsurance) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateDeeplinkInsuranceStatus(DeeplinkInsuranceDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblDeeplinkInsurance tblDeeplinkInsurance = new TblDeeplinkInsurance();
        tblDeeplinkInsurance.setId(dto.getId());
        tblDeeplinkInsurance.setStatus(dto.getStatus());
        if (tblDeeplinkInsuranceMapper.updateByPrimaryKeySelective(tblDeeplinkInsurance) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<DeeplinkInsurancePageQueryVo>, Object> findPageDeeplinkInsurance(DeeplinkInsuranceDto dto) {
        //检查值 - 不在范围则设置为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        dto.setMobilePlatform(Optional.ofNullable(dto.getMobilePlatform()).filter(s -> s > -1 && s < 2).orElse(null));
        dto.setType(Optional.ofNullable(dto.getType()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<DeeplinkInsurancePageQueryVo> list = tblDeeplinkInsuranceMapper.findPageDeeplinkInsurance(dto);
        PageInfo<DeeplinkInsurancePageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<List<DeeplinkInsuranceListSelectVo>, Object> listSelect() {
        List<DeeplinkInsuranceListSelectVo> list = tblDeeplinkInsuranceMapper.listSelect();
        return ResponseResult.success(list);
    }
}
